<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *任务管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_renwu_task_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$starttime=strtotime($item['starttime']);
			$endtime=strtotime($item['endtime']);
			if($starttime<=time()&&time()<=$endtime){
				$item['status']=1;
			}
			elseif($starttime>time()){
				$item['status']=0;
			}
			elseif(time()>$endtime){
				$item['status']=2;
			}
		}
	}	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'任务列表'),
			array('op'=>'edit','title'=>'添加任务'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'displayorder','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'type','title'=>'任务类型','type'=>'radio','options'=>'type'),
			array('name'=>'starttime','title'=>'开始时间','type'=>'string'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string','width'=>'40'),
		)
	);
	$return['status']=array('0'=>'未开始','1'=>'进行中','2'=>'已结束');
	$return['type']=array('poster'=>'任务海报','info_phone'=>'绑定手机','order_first'=>'首次购物','recharge_full'=>'单笔充值满额','order_full'=>'单笔满额','order_all'=>'累计消费','pyramid_money'=>'分销佣金','pyramid_num'=>'下级人数','comment'=>'商品好评','post'=>'社区发帖','goods'=>'购买指定商品','recharge_count'=>'累计充值满额');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_renwu_task_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	//$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	//$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	
	$reward=json_decode($item['reward'],true);
	$item['credit1']=$reward['credit1'];
	$item['credit2']=$reward['credit2'];
	$item['red']=$reward['red'];
	$item['reward_coupon']=$reward['reward_coupon'];
	$item['reward_goods']=$reward['reward_goods'];
	
	$followreward=json_decode($item['followreward'],true);
	$item['follow_credit1']=$followreward['follow_credit1'];
	$item['follow_credit2']=$followreward['follow_credit2'];
	$item['follow_red']=$followreward['follow_red'];
	$item['follow_coupon']=$followreward['follow_coupon'];
	
	$reward2=json_decode($item['reward2'],true);
	$item['demand2']=$reward2['demand2'];
	$item['reward2_credit1']=$reward2['reward2_credit1'];
	$item['reward2_credit2']=$reward2['reward2_credit2'];
	$item['reward2_red']=$reward2['reward2_red'];
	$item['reward2_coupon']=$reward2['reward2_coupon'];
	$item['reward2_goods']=$reward2['reward2_goods'];

	$reward3=json_decode($item['reward3'],true);
	$item['demand3']=$reward3['demand3'];
	$item['reward3_credit1']=$reward3['reward3_credit1'];
	$item['reward3_credit2']=$reward3['reward3_credit2'];
	$item['reward3_red']=$reward3['reward3_red'];
	$item['reward3_coupon']=$reward3['reward3_coupon'];
	$item['reward3_goods']=$reward3['reward3_goods'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回任务列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本设置',
							'list'=>array(
								array('name'=>'type','title'=>'任务类型','type'=>'select','options'=>'type','option_title'=>'type_name','option_value'=>'type_key'),
								array('type'=>'string','title'=>'任务名称','name'=>'title','help'=>''),
								array('type'=>'image','title'=>'任务图片','name'=>'image','help'=>''),
								array('title'=>'选择海报','name'=>'posterid','type'=>'select','options'=>'posterid','show'=>array('type'=>'poster')),	
								array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
								array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'poster'),'fields'=>array(
									array('type'=>'string','title'=>'转发海报并吸引'),
									array('type'=>'string','name'=>'demand'),
									array('type'=>'string','title'=>'人关注'),
								)),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'recharge_full'),'fields'=>array(
									array('type'=>'string','title'=>'单笔满额'),
									array('type'=>'string','name'=>'demand'),
									array('type'=>'string','title'=>'元'),
								)),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'order_all'),'fields'=>array(
									array('type'=>'string','title'=>'累计满额'),
									array('type'=>'string','name'=>'demand'),
									array('type'=>'string','title'=>'元'),
								)),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'recharge_count'),'fields'=>array(
									array('type'=>'string','title'=>'累计充值满额'),
									array('type'=>'string','name'=>'demand'),
									array('type'=>'string','title'=>'元'),
								)),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'pyramid_num'),'fields'=>array(
									array('type'=>'string','title'=>'下级人数满'),
									array('type'=>'string','name'=>'demand'),
									array('type'=>'string','title'=>'人'),
								)),
								array('type'=>'string_much','title'=>'任务需求','show'=>array('type'=>'comment'),'fields'=>array(
									array('type'=>'string','title'=>'给商品好评即可完成任务'),
								)),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'转发海报并吸引','type'=>'string_two','unit1'=>'人关注','show'=>array('type'=>'poster')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'单笔充值满额','type'=>'string_two','unit1'=>'元','show'=>array('type'=>'recharge_full')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'单笔满额','type'=>'string_two','unit1'=>'元','show'=>array('type'=>'order_full')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'累计满额','type'=>'string_two','unit1'=>'元','show'=>array('type'=>'order_all')),
								array('title'=>'任务需求:购买指定商品','name'=>'demand','type'=>'choose','action'=>'get_goods','show'=>array('type'=>'goods')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'累计充值满额','type'=>'string_two','unit1'=>'元','show'=>array('type'=>'recharge_count')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'下级人数满','type'=>'string_two','unit1'=>'人','show'=>array('type'=>'pyramid_num')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'给商品好评即可完成任务','type'=>'string_two','unit1'=>'','show'=>array('type'=>'comment')),
							//	array('title'=>'任务需求','name1'=>'demand','title1'=>'在社区中发表指定篇帖子，即可完成任务获得奖励','type'=>'string_two','unit1'=>'','show'=>array('type'=>'post')),
								array('type'=>'radio','title'=>'截止时间','name'=>'stop_type','options'=>'stop_type','help'=>'完成任务的截止时间与此时间段有关'),
								//array('title'=>'按限时','name1'=>'stop_limit','title1'=>'自接任务起必须在','type'=>'string_two','unit1'=>'小时内完成','show'=>array('stop_type'=>1)),
									array('type'=>'string_much','title'=>'按限时','show'=>array('stop_type'=>1),'fields'=>array(
									array('type'=>'string','title'=>'自接任务起必须在'),
									array('type'=>'string','name'=>'stop_limit'),
									array('type'=>'string','title'=>'小时内完成'),
								)),
								array('title'=>'按日期','name'=>'stop_time','type'=>'time','show'=>array('stop_type'=>2),'help'=>'自接任务起必须在该日期前完成'),
								array('title'=>'按周期','name'=>'stop_cycle','type'=>'select','options'=>'stop_cycle','help'=>'自接任务起必须在该周期内完成','show'=>array('stop_type'=>3)),
								array('type'=>'radio','title'=>'重复领取','name'=>'repeat_type','options'=>'repeat_type','help'=>'完成任务或任务失败后间隔多久可重复领取此任务'),
								//array('title'=>'按间隔','name1'=>'repeat_interval','title1'=>'自接完成任务或任务失败起，必须','type'=>'string_two','unit1'=>'小时后才能再领取此任务','show'=>array('repeat_type'=>2)),
								array('type'=>'string_much','title'=>'按间隔','show'=>array('repeat_type'=>2),'fields'=>array(
									array('type'=>'string','title'=>'自接完成任务或任务失败起，必须'),
									array('type'=>'string','name'=>'repeat_interval'),
									array('type'=>'string','title'=>'小时后才能再领取此任务'),
								)),
								array('title'=>'按周期','name'=>'repeat_cycle','type'=>'select','options'=>'repeat_cycle','show'=>array('repeat_type'=>3),'help'=>'自接完成任务或任务失败起，必须在该周期才能再领取任务'),
								
							)
						),
						array(
							'title'=>'完成奖励',
							'list'=>array(
							//	array('title'=>'商城积分','name1'=>'credit1','title1'=>'奖励','type'=>'string_two','unit1'=>'个积分'),
							//	array('title'=>'商城余额','name1'=>'credit2','title1'=>'奖励','type'=>'string_two','unit1'=>'元余额'),
							//	array('title'=>'微信红包','name1'=>'red','title1'=>'奖励','type'=>'string_two','unit1'=>'元红包'),
									array('type'=>'string_much','title'=>'商城积分','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'credit1'),
									array('type'=>'string','title'=>'个积分'),
								)),
								array('type'=>'string_much','title'=>'商城余额','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'credit2'),
									array('type'=>'string','title'=>'元余额'),
								)),
								array('type'=>'string_much','title'=>'微信红包','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'red'),
									array('type'=>'string','title'=>'元红包'),
								)),
								array('title'=>'选择优惠券','name'=>'reward_coupon','type'=>'choose','action'=>'get_coupon','choose_title'=>'选择优惠券'),
								array('title'=>'特价商品','name'=>'reward_goods','type'=>'choose','action'=>'get_goods','choose_title'=>'选择商品'),
								
							)
						),
						array(
							'title'=>'关注人奖励',
							'list'=>array(
						//		array('title'=>'商城积分','name1'=>'follow_credit1','title1'=>'奖励','type'=>'string_two','unit1'=>'个积分'),
						//		array('title'=>'商城余额','name1'=>'follow_credit2','title1'=>'奖励','type'=>'string_two','unit1'=>'元余额'),
						//		array('title'=>'微信红包','name1'=>'follow_red','title1'=>'奖励','type'=>'string_two','unit1'=>'元红包'),
								array('type'=>'string_much','title'=>'商城积分','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'follow_credit1'),
									array('type'=>'string','title'=>'个积分'),
								)),
								array('type'=>'string_much','title'=>'商城余额','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'follow_credit2'),
									array('type'=>'string','title'=>'元余额'),
								)),
								array('type'=>'string_much','title'=>'微信红包','fields'=>array(
									array('type'=>'string','title'=>'奖励'),
									array('type'=>'string','name'=>'follow_red'),
									array('type'=>'string','title'=>'元红包'),
								)),
								array('title'=>'选择优惠券','name'=>'follow_coupon','type'=>'choose','action'=>'get_coupon','choose_title'=>'选择优惠券'),
								
							)
						),
	
						array(
							'title'=>'二级海报',
							'show'=>array('type'=>1),
							'list'=>array(
								array('title'=>'二级需求','name1'=>'demand2','title1'=>'扫描并吸引','type'=>'string_two','unit1'=>'个人关注','show'=>array('type'=>1)),
								array('title'=>'商城积分','name1'=>'reward2_credit1','title1'=>'奖励','type'=>'string_two','unit1'=>'个积分'),
								array('title'=>'商城余额','name1'=>'reward2_credit2','title1'=>'奖励','type'=>'string_two','unit1'=>'元余额'),
								array('title'=>'微信红包','name1'=>'reward2__red','title1'=>'奖励','type'=>'string_two','unit1'=>'元红包'),
								array('title'=>'选择优惠券','name'=>'reward2_coupon','type'=>'choose','action'=>'get_coupon','choose_title'=>'选择优惠券'),
								array('title'=>'特价商品','name'=>'reward2_goods','type'=>'choose','action'=>'get_goods','choose_title'=>'选择商品'),
								
							)
						),
						array(
							'title'=>'三级海报',
							'show'=>array('type'=>1),
							'list'=>array(
								array('title'=>'三级需求','name1'=>'demand3','title1'=>'扫描并吸引','type'=>'string_two','unit1'=>'个人关注','show'=>array('type'=>1)),
								array('title'=>'商城积分','name1'=>'rewrad3_credit1','title1'=>'奖励','type'=>'string_two','unit1'=>'个积分'),
								array('title'=>'商城余额','name1'=>'rewrad3_credit2','title1'=>'奖励','type'=>'string_two','unit1'=>'元余额'),
								array('title'=>'微信红包','name1'=>'rewrad3_red','title1'=>'奖励','type'=>'string_two','unit1'=>'元红包'),
								array('title'=>'选择优惠券','name'=>'rewrad3_coupon','type'=>'choose','action'=>'get_coupon','choose_title'=>'选择优惠券'),
								array('title'=>'特价商品','name'=>'reward3_goods','type'=>'choose','action'=>'get_goods','choose_title'=>'选择商品'),
								
							)
						),
			
			)
	);
	$return['stop_cycle']=array(array('id'=>'1','title'=>'本日'),array('id'=>'2','title'=>'本周'),array('id'=>'3','title'=>'本月'));
	$return['repeat_cycle']=array(array('id'=>'1','title'=>'明日'),array('id'=>'2','title'=>'下周'),array('id'=>'3','title'=>'下月'));
	$return['stop_type']=array('0'=>'无限制','1'=>'按限时','2'=>'按日期','3'=>'按周期');
	$return['repeat_type']=array('0'=>'无限制','1'=>'不可重复','2'=>'按间隔','3'=>'按周期');
	$return['headstype']=array('1'=>'优惠金额','2'=>'优惠折扣');
	$return['type']=pdo_getall('jy_renwu_task_type',array(),array('type_key','type_name'));
	if($this->plugin_exists('jy_poster')){
		$return['posterid']=pdo_getall('jy_poster_poster',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_renwu'),array('id','title'));
	}else{
		unset($htmls['edits'][0]['list'][3]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$reward=array(
		'credit1'=>intval($postdata['credit1']),
		'credit2'=>intval($postdata['credit2']),
		'red'=>intval($postdata['red']),
		'reward_coupon'=>intval($postdata['reward_coupon']),
		'reward_goods'=>intval($postdata['reward_goods']),
	);
	$followreward=array(
		'follow_credit1'=>intval($postdata['follow_credit1']),
		'follow_credit2'=>intval($postdata['follow_credit2']),
		'follow_red'=>intval($postdata['follow_red']),
		'follow_coupon'=>intval($postdata['follow_coupon']),
	);
	$reward2=array(
		'demand2'=>intval($postdata['demand2']),
		'reward2_credit1'=>intval($postdata['reward2_credit1']),
		'reward2_credit2'=>intval($postdata['reward2_credit2']),
		'reward2_red'=>intval($postdata['reward2_red']),
		'reward2_coupon'=>intval($postdata['reward2_coupon']),
		'reward2_goods'=>intval($postdata['reward2_goods']),
	);
	$reward3=array(
		'demand3'=>intval($postdata['demand3']),
		'reward3_credit1'=>intval($postdata['reward3_credit1']),
		'reward3_credit2'=>intval($postdata['reward3_credit2']),
		'reward3_red'=>intval($postdata['reward3_red']),
		'reward3_coupon'=>intval($postdata['reward3_coupon']),
		'reward3_goods'=>intval($postdata['reward3_goods']),
	);
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'type'=>trim($postdata['type']),
		'demand'=>intval($postdata['demand']),
		'posterid'=>intval($postdata['posterid']),
		'stop_type'=>intval($postdata['stop_type']),
		'stop_limit'=>intval($postdata['stop_limit']),
		'stop_time'=>trim($postdata['stop_time']),
		'stop_cycle'=>intval($postdata['stop_cycle']),
		'repeat_type'=>intval($postdata['repeat_type']),
		'repeat_interval'=>intval($postdata['repeat_interval']),
		'repeat_cycle'=>intval($postdata['repeat_cycle']),
		'reward'=>ijson_encode($reward),
		'reward2'=>ijson_encode($reward2),
		'reward3'=>ijson_encode($reward3),
		'followreward'=>ijson_encode($followreward),
		'endtime'=>$postdata['endtime'],
		'starttime'=>$postdata['starttime'],
	);
	if(!empty($reward2)){
		$data['level2']=1;
	}
	if(!empty($reward3)){
		$data['level3']=1;
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_renwu_task_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_renwu_task_list',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_renwu_task_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的任务！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_renwu_task_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
